package reflecttest;

import java.lang.reflect.Field;

class Student {
	String name;
	int age;

	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;

	}
}

public class FieldTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student stu1 = new Student("chen", 55);
		Student stu2 = new Student("yang", 66);
		System.out.println(compare(stu1, stu2).name + " is bigger");

	}

	private static Student compare(Student stu1, Student stu2) {
		try {
			Field field = stu1.getClass().getDeclaredField("age");
			// field = Student.class.getDeclaredField("age");

			int age1 = (int) field.get(stu1);
			int age2 = (int) field.get(stu2);

			if (age1 > age2) {
				return stu1;
			} else {
				return stu2;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}

}
